﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Simhopp.Database;

namespace Simhopp.Model
{
    public class Diver : InterfaceDiverModel
    {

        public int ID { get; set; }
        public string Name { get; set; }
        public int StartNumber { get; set; }
        public double TotalPoints { get; set; }
        public Countries Nationality { get; set; }
        public List<Dive> DiveList { get; set; }
        public DateTime DateOfBirth { get; set; }
        public char Gender { get; set; }
        public int C_ID { get; set; }

        public Diver()
        {
            this.ID = 0;
            this.Name = "";
            this.StartNumber = 0;
            this.TotalPoints = 0;
            this.Gender = ' ';
            this.Nationality = Countries.Afghanistan;
            this.DateOfBirth = new DateTime(2000, 1, 1);
            DiveList = new List<Dive>();
        }

        public Diver(int ID, string Name, DateTime DateOfBirth, char Gender,
            int StartNumber, double TotalPoints, Countries Nationality)
        {
            this.ID = ID;
            this.Name = Name;
            this.DateOfBirth = DateOfBirth;
            this.StartNumber = StartNumber;
            this.TotalPoints = TotalPoints;
            this.Nationality = Nationality;
            this.Gender = Gender;
            DiveList = new List<Dive>();
            //GetDivesForDiver(DiveList, this.ID);
        }

        public void AddDive(int ID, string Name, double Difficulty, string NameCode, int DiveNumber)
        {
            DBConnect dbc = new DBConnect();
            DiveList.Add(new Dive(ID, this.C_ID, this.ID, Name, Difficulty, NameCode, DiveNumber));
            dbc.AddDiveToDiver(ID, this.ID, DiveNumber,0);
        }

        public void AddDive(int ID, string Name, double Difficulty, string NameCode, double Score, int DiveNumber)
        {
            DiveList.Add(new Dive(ID, this.C_ID, this.ID, Name, Difficulty, NameCode, DiveNumber));
        }

        public void AddScoreAndJudgeIDToJudgeScoreDict(int JudgeID, int DiveNr, double score)
        {
            Dive dive = GetDiveByNumber(DiveNr);
            if (dive != null)
            {
                dive.JudgeScore.Add(JudgeID, score);
            }
        }
        public void CalculateTotalScoreForDive(int DiveNr)
        {
            Dive dive = GetDiveByNumber(DiveNr);
            if(dive != null)
                dive.CalculateDiveScore();
        }
        public Dictionary<int, double> GetJudgeScoreForDive(int DiveNr)
        {
            Dive dive = GetDiveByNumber(DiveNr);
            if (dive != null)
                return dive.JudgeScore;
            return null;
        }
        public Dive GetDiveByNumber(int DiveNr)
        {
            foreach (var dive in DiveList)
            {
                if (dive.DiveNumber == DiveNr)
                    return dive;
            }
            return null;
        }

        public Dive GetDive(int ID)
        {
            foreach (var dive in DiveList)
            {
                if (dive.ID == ID)
                    return dive;
            }
            return null;
        }

        public void RemoveDive(int ID)
        {
            Dive dive = GetDive(ID);
            if (dive != null)
                DiveList.Remove(dive);
            DBConnect dbc = new DBConnect();
            dbc.RemoveDiveFromDiveList(this.ID, dive.ID);
        }

        public void UpdateDive(int ID, string Name, double Difficulty, string NameCode, double Score, int DiveNumber)
        {
            Dive dive = GetDive(ID);
            if (dive != null)
            {
                dive.Name = Name;
                dive.Difficulty = Difficulty;
                dive.NameCode = NameCode;
                dive.Score = Score;
                dive.DiveNumber = DiveNumber;
            }
        }

        public void CalculateTotalPoints()
        {
            TotalPoints = 0;
            foreach (var dive in DiveList)
            {
                TotalPoints += dive.Score;
            }
        }

        public void GetDivesForDiver(DBConnect dbc)
        {
            //DBConnect dbc = new DBConnect();
            dbc.GetDivesForDiver(this.DiveList, this.ID);
        }
    }
}


